# -*- coding: utf-8 -*-

#    Copyright (C) 2005, 2006, 2007 2008, 2009, 2010, 2011 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>
#    This file is part of Promogest.

#    Promogest is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    Promogest is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with Promogest.  If not, see <http://www.gnu.org/licenses/>.

import hashlib
import datetime
#from promogest.lib.utils import Response
from sqlalchemy.orm import *
from promogest.dao.User import User
from promogest.lib.relativedelta import relativedelta
#from promogest.lib.utils import *
from promogest.lib.page import Page
import simplejson as json
from promogest.lib.MailHandler import SendMail

def pp_ipn(req=None, static=None, subdomain=None):
    """
{'last_name': u'Smith',
'txn_id': u'16102509',
'receiver_email': u'seller@paypalsandbox.com',
'payment_status': u'Completed',
'tax': u'2.02',
'residence_country': u'IT',
'invoice': u'abc1234',
'address_state': u'CA',
'payer_status': u'verified',
'txn_type': u'web_accept', 'address_country': u'Italy',
'payment_date': u'17:09:16 Oct 24, 2011 PDT', 'first_name': u'John',
'item_name': u'something', 'address_street': u'123, any street',
'charset': u'windows-1252', 'custom': u'xyz123', 'notify_version': u'2.1',
'address_name': u'John Smith', 'test_ipn': u'1', 'item_number': u'AK-1234',
'receiver_id': u'TESTSELLERID1', 'business': u'seller@paypalsandbox.com',
'payer_id': u'TESTBUYERID01',
'verify_sign': u'A-z4mReySQhBKc2wA95gHD.r.U3hABbnM3c8m7jBhlIk7tSSC-6KY4xL',
'address_zip': u'95131', 'address_country_code': u'IT',
'address_city': u'San Jose', 'address_status': u'confirmed',
'mc_fee': u'0.44', 'mc_currency': u'EUR', 'shipping': u'3.04',
'payer_email': u'buyer@paypalsandbox.com', 'payment_type': u'echeck',
'mc_gross': u'12.34', 'quantity': u'1'}

    """

    #print req.form.to_dict()
    if req.form.get('payment_status') =='Completed' or (req.form.get('payment_status') =='Pending' and req.form.get('pending_reason') =='multi_currency'):
        #print "Genero codice ed invio"
        a = json.loads(req.form.get('custom'))
        print "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", a
        #from promogest.
        #email = a[2]
        #azienda = ragione_sociale[0:20].strip().replace(" ","").lower()


        #pageData={
            #"codice" : str(hashlib.sha224(azienda+orda(azienda)).hexdigest()),
            #"azienda" : azienda,
            #"ragione_sociale": ragione_sociale,
            #"tipo" : tipo,
        #}
        #html = renderizza(req,"codice_attivazione.html", pageData)
        #SendMail(req=req,to=email).sendStdHTMLMail(html=html, subject="Codice Attivazione PromoGest")
    #else:
        #print " PAGAMENTO NON EFFETTUATO"

    redirectUrl="/main"
    return Page(req).redirect(redirectUrl)
